﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL ;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;


namespace dhSystemBLL.produceBLL
{
   public  class pdrkBLL
    {
        private V_pdrkTableAdapter  _pdrkAdapter = null;
        private V_pdrkTableAdapter pdrkAdapter
        {
            get
            {
                if (_pdrkAdapter == null) _pdrkAdapter = new V_pdrkTableAdapter();
                return _pdrkAdapter;

            }
        }
       private pdrkMasterTableAdapter _pdrkMasterlAdapter = null;
       private pdrkMasterTableAdapter pdrkMasterAdapter
        {
            get
            {
                if (_pdrkMasterlAdapter == null) _pdrkMasterlAdapter = new pdrkMasterTableAdapter();
                return _pdrkMasterlAdapter;

            }
        }
       private pdrkDetailTableAdapter _pdrkDetailAdapter = null;
       private pdrkDetailTableAdapter pdrkDetailAdapter
        {
            get
            {
                if (_pdrkDetailAdapter == null) _pdrkDetailAdapter = new pdrkDetailTableAdapter();
                return _pdrkDetailAdapter;

            }
        }
       /// <summary>
       /// 查询视图表
       /// </summary>
       /// <returns></returns>

       public ViewDataSet.V_pdrkDataTable getV_pdrkALL()
       {
           try
           {
               return pdrkAdapter.GetDataV_pdrkALL ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 查询胚带入库主表
        /// </summary>
        /// <returns></returns>

       public produceDataSet.pdrkMasterDataTable getpdrkMasterAll()
        {
            try
            {
                return pdrkMasterAdapter.GetDatapdrkMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询胚带入库从表
        /// </summary>
        /// <returns></returns>

       public produceDataSet.pdrkDetailDataTable getpdrkDetailAll()
        {
            try
            {
                return pdrkDetailAdapter.GetDatapdrkDetailAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据入库单号查询胚带入库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public produceDataSet.pdrkMasterDataTable getpdrkMasterAllBybillNo(string billNo)
        {
            try
            {
                return pdrkMasterAdapter.GetDatapdrkMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据入库单号查询胚带入库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public produceDataSet.pdrkDetailDataTable getspdrkDetailAllBybillNo(string billNo)
        {
            try
            {
                return pdrkDetailAdapter.GetDatapdrkDetailBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改胚带入库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int pdrkInsert(produceDataSet.pdrkMasterDataTable masterTable, produceDataSet.pdrkDetailDataTable pTable, string state, string billNo, string Date)
        {
            pdrkDetailModel pdrkDetailRow = new pdrkDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pdrkMasterDAL pdrkMaster = new pdrkMasterDAL(tn, conn);
                pdrkDetailDAL pdrkDetail = new pdrkDetailDAL(tn, conn);

                //produceDataSet.pdrkMasterDataTable masterTable = new produceDataSet.pdrkMasterDataTable();
                //produceDataSet.pdrkMasterRow masterRow = masterTable.NewpdrkMasterRow();
                //masterRow.billNo = row.billNo;
                //masterRow.billDate = row.billDate;
                //masterRow.billStatus = row.billStatus;
                //masterRow.createDate = row.createDate;
                //masterRow.createUser = row.createUser;
                //masterRow.deptNum = row.deptNum;
                //masterRow.stockname = row.stockname;
                //masterRow.updateDate = row.updateDate;
                //masterRow.updateUser = row.updateUser;
                //masterRow.workername = row.workername;
                //masterRow.mark = row.mark;
                //masterTable.AddpdrkMasterRow(masterRow);
                //pTable.AcceptChanges();
                //masterTable.AcceptChanges();
                pdrkMaster.pdrkMasterInsertII(masterTable, pTable);

                
                //basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    produceDataSet.pdrkMasterDataTable pdrkTable = getpdrkMasterAllBybillNo(billNo);
                //    if (pdrkTable.Rows.Count == 0)
                //    {
                //        pdrkMaster.pdrkMasterInsertII (row);
                //    }
                //    else
                //    {
                //        row.billNo = "P" + Date + "-" + basicSql.getOrderSerail("胚带入库单号");
                //        pdrkMaster.pdrkMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("胚带入库单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "胚带入库单号");
                //}
                //if (state == "编辑")
                //{
                //    pdrkMaster.pdrkMasterUpdate(row);
                //}


                //foreach (produceDataSet.pdrkDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        pdrkDetailRow.recordid = detailRow.recordid;
                //        pdrkDetailRow.billNo = detailRow.billNo;
                //        pdrkDetailRow.pName  = detailRow.pName ;
                //        pdrkDetailRow.qty = detailRow.qty;
                //        pdrkDetailRow.unit = detailRow.unit;
                //        pdrkDetailRow.createDate = DateTime.Now;
                //        if (!detailRow.IsmarkNull())
                //        {
                //            pdrkDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            pdrkDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        pdrkDetail.pdrkMasterInsertII (pdrkDetailRow);
                     

                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        pdrkDetail.pdrkDetailUpdate(pdrkDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        pdrkDetail.pdrkDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            //catch (SqlException ex)
            //{
            //    tn.Rollback();
            //    if (ex.Number == 2627)
            //    {
            //        throw new Exception("这个入库单号已经存在");
            //    }


            //    throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            //}
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


       /// 删除一行胚带入库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int pdrkMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pdrkMasterDAL pdrkMaster = new pdrkMasterDAL(tn, conn);
                pdrkMaster.pdrkDel(billNo);
              //  pdrkMaster.pdrkMasterDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
